package com.apexedu.portal.controller;

import cn.yiban.open.Uis;
import com.apexedu.biz.Const;
import com.apexedu.framework.dao.BaseDao;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.SysPropertiesUtil;
import com.apexedu.identity.IdConstants;
import com.apexedu.identity.entity.TSysUser;
import com.apexedu.portal.entity.YibanStudent;
import com.apexedu.portal.entity.YibanTeacher;
import com.apexedu.portal.util.LittleTimer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 门户4.0展示界面
 * Created by ly on 2015/4/10.
 */
@Controller
public class MhInterfaceController extends BaseController {

    @Resource
    private BaseDao baseDao;

    /**
     * 套用iframe界面
     *
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(value =  "/uc/yiban", method = RequestMethod.GET)
    public void yiban(HttpServletResponse response,HttpServletRequest request, ModelMap map) {
        TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
        /**
         * 使用时，需要用以下命令将RSA的私钥转为PKCS8格式
         *
         * # openssl pkcs8 -topk8 -in RSA的私钥文件 -out pkcs8_rsa_private_key.pem -nocrypt
         */
        debug("<<{}>> yiban start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        String school_id = SysPropertiesUtil.get(Const.MH_YIBAN_SCID);
        String priv_file = SysPropertiesUtil.get(Const.MH_YIBAN_CERTIFICATION_PATH);
        Map<String, String> result = new HashMap<String, String>();
        try{
            Uis uis = Uis.getInstance();
            uis.setup(response, school_id, priv_file);

            if("1".equals(user.getUsertype())){//教师
                String sql=" select name,teacher_id,status_id,role,college,sex,specialty,eclass,native_place,phone,"+
                           " to_char(SYSDATE - TO_DATE('1970-1-1 8', 'YYYY-MM-DD HH24')) * 86400000 + TO_NUMBER(TO_CHAR(SYSTIMESTAMP(3), 'FF')) as build_time"+
                           " from apexu_jw.yb_jsxx "+
                           " where userid=? ";
                YibanTeacher yibanTeacher= DBUtil.queryBean(sql, YibanTeacher.class, user.getUserloginid());
                if (yibanTeacher!=null) {
                    result.put("name", yibanTeacher.getName());
                    result.put("teacher_id", yibanTeacher.getTeacher_id());
                    result.put("status_id", yibanTeacher.getStatus_id());
                    result.put("role", yibanTeacher.getRole());
                    result.put("college", yibanTeacher.getCollege());
                    result.put("sex", yibanTeacher.getSex());
                    result.put("specialty", yibanTeacher.getSpecialty());
                    result.put("eclass", yibanTeacher.getEclass());
                    result.put("native_place", yibanTeacher.getNative_place());
                    result.put("phone", yibanTeacher.getPhone());
                    result.put("build_time", yibanTeacher.getBuild_time());
                    System.out.println("该账号的老师信息：" + yibanTeacher.getName());
                }else{
                    System.out.println("未查到该账号的老师信息："+user.getUserloginid());
                }
            }else if ("2".equals(user.getUsertype())||1==1){//学生
                String sql= " select name,student_id,exam_id,enter_id,status_id,enter_year,instructor_id,status,schooling,education,"+
                            " role,college,sex,specialty,eclass,native_place,phone,"+
                            " to_char(SYSDATE - TO_DATE('1970-1-1 8', 'YYYY-MM-DD HH24')) * 86400000 + TO_NUMBER(TO_CHAR(SYSTIMESTAMP(3), 'FF')) as build_time"+
                            " from apexu_jw.yb_xsxx "+
                            " where userid=? ";
                YibanStudent yibanStudent=DBUtil.queryBean(sql, YibanStudent.class,user.getUserloginid());
                if (yibanStudent!=null) {
                    result.put("name", yibanStudent.getName());
                    result.put("student_id", yibanStudent.getStudent_id());
                    result.put("exam_id", yibanStudent.getExam_id());
                    result.put("enter_id", yibanStudent.getEnter_id());
                    result.put("status_id", yibanStudent.getStatus_id());
                    result.put("enter_year", yibanStudent.getEnter_year());
                    result.put("instructor_id", yibanStudent.getInstructor_id());
                    result.put("status", yibanStudent.getStatus());
                    result.put("schooling", yibanStudent.getSchooling());
                    result.put("education", yibanStudent.getEducation());
                    result.put("role", yibanStudent.getRole());
                    result.put("college", yibanStudent.getCollege());
                    result.put("sex", yibanStudent.getSex());
                    result.put("specialty", yibanStudent.getSpecialty());
                    result.put("eclass", yibanStudent.getEclass());
                    result.put("native_place", yibanStudent.getNative_place());
                    result.put("phone", yibanStudent.getPhone());
                    result.put("build_time", yibanStudent.getBuild_time());
                    System.out.println("该账号的学生信息：" + yibanStudent.getName());
                }else{
                    System.out.println("未查到该账号的学生信息："+user.getUserloginid());
                }
            }else{
                result.put("name", "测试");
                result.put("student_id", user.getUserloginid());
                result.put("exam_id", "测试");
                result.put("enter_id", "");
                result.put("status_id", "");
                result.put("enter_year", "");
                result.put("instructor_id", "");
                result.put("status","");
                result.put("schooling", "");
                result.put("education", "");
                result.put("role", "");
                result.put("college", "");
                result.put("sex", "");
                result.put("specialty", "");
                result.put("eclass","");
                result.put("native_place", "");
                result.put("phone", "");
                result.put("build_time", "");
                System.out.println("该账号的无对应类型信息：" +"测试");
            }
            uis.run(result, false);
        }catch(Exception e){
            error("<<{}>> yiban error : {}", this.getClass().getSimpleName(), e);
            System.out.println("/////////////////////////////////////////yiban-error-begin/////////////////////////////////////////");
            System.out.println(e.toString());
            System.out.println("/////////////////////////////////////////yiban-error-end/////////////////////////////////////////");
            throw new RuntimeException(e);
        }
    }
}
